//
//  ViewController.m
//  MutilScrollView
//
//  Created by wsl 王 on 2019/1/21.
//  Copyright © 2019年 wsl 王. All rights reserved.
//

#import "ViewController.h"
#import "SubScrollView.h"
@interface ViewController ()<UIScrollViewDelegate>

@property (nonatomic,strong) SubScrollView *baseScrollView;
@property (nonatomic,strong) SubScrollView *subScrollView;


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.baseScrollView = [[SubScrollView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:self.baseScrollView];
    
    self.baseScrollView.contentSize = CGSizeMake(0, self.view.bounds.size.height + 300);
    self.baseScrollView.backgroundColor = [UIColor yellowColor];
    self.baseScrollView.canScroll = YES;
    self.baseScrollView.delegate = self;
    
    
    self.subScrollView = [[SubScrollView alloc] initWithFrame:CGRectMake(0, 300, self.view.frame.size.width, self.view.frame.size.height + 300)];
    self.baseScrollView.allowGestureEventPassViews = @[self.subScrollView];
    [self.baseScrollView addSubview:self.subScrollView];
    self.subScrollView.contentSize = CGSizeMake(0, self.view.bounds.size.height * 2);
    self.subScrollView.supScrollView = self.baseScrollView;
    self.subScrollView.delegate = self;
    UIView * bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, 60, self.view.frame.size.width, 50)];
    bottomView.backgroundColor = [UIColor whiteColor];
    [self.subScrollView addSubview:bottomView];
    self.subScrollView.backgroundColor = [UIColor redColor];
    
}

- (void)scrollViewDidScroll:(SubScrollView *)scrollView
{
    if (scrollView == self.baseScrollView) {
        
        //父类
        CGFloat contentOffset = 300;
        
        if (!scrollView.canScroll) {
            // 这里通过固定contentOffset的值，来实现不滚动
            scrollView.contentOffset = CGPointMake(0, contentOffset);
        } else if (scrollView.contentOffset.y >= contentOffset) {
            scrollView.contentOffset = CGPointMake(0, contentOffset);
            scrollView.canScroll = NO;
            
            // 通知delegate内容开始可以滚动
            self.subScrollView.canScroll = YES;
        }
        scrollView.showsVerticalScrollIndicator = scrollView.canScroll;
    } else {
        
        //子类
        if (!scrollView.canScroll) {
            // 这里通过固定contentOffset，来实现不滚动
            scrollView.contentOffset = CGPointZero;
        } else if (scrollView.contentOffset.y <= 0) {
            scrollView.canScroll = NO;
            // 通知容器可以开始滚动
            scrollView.supScrollView.canScroll = YES;
        }
        scrollView.showsVerticalScrollIndicator = scrollView.canScroll;
    }
}


@end
